package com.buli.minio.service;


import io.minio.errors.MinioException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.util.List;
import java.util.Map;

@Service
public class FileService {

    @Autowired
    private MinioService minioService;



    /**
     * 单个文件上传
     *
     * @param file MultipartFile
     * @return 文件 URL
     * @throws IOException
     * @throws NoSuchAlgorithmException
     * @throws InvalidKeyException
     * @throws MinioException
     */
    public String uploadSingleFile(MultipartFile file) throws IOException, NoSuchAlgorithmException, InvalidKeyException, MinioException {


        // 上传文件
        String fileUrl = minioService.uploadSingleFile(file);
        //TODO 可以将上传文件记录进行留痕

        return fileUrl;
    }

    /**
     * 批量文件上传
     *
     * @param files List<MultipartFile>
     * @return 文件 URL 列表
     * @throws IOException
     * @throws NoSuchAlgorithmException
     * @throws InvalidKeyException
     * @throws MinioException
     */
    public List<String> uploadMultipleFiles(List<MultipartFile> files) throws IOException, NoSuchAlgorithmException, InvalidKeyException, MinioException {
        List<String> fileUrls = minioService.uploadMultipleFiles(files);

        for (MultipartFile file : files) {
            uploadSingleFile(file);
        }

        return fileUrls;
    }

    /**
     * 单个文件下载
     *
     * @param fileName 文件名
     * @return 文件 URL
     * @throws IOException
     * @throws NoSuchAlgorithmException
     * @throws InvalidKeyException
     * @throws MinioException
     */
    public String downloadSingleFile(String fileName) throws IOException, NoSuchAlgorithmException, InvalidKeyException, MinioException {
        return minioService.downloadSingleFile(fileName);
    }

    /**
     * 批量文件下载为 ZIP 压缩包
     *
     * @param fileNames 文件名列表
     * @return ZIP 文件的 URL
     * @throws IOException
     * @throws NoSuchAlgorithmException
     * @throws InvalidKeyException
     * @throws MinioException
     */
    public String downloadMultipleFilesAsZip(List<String> fileNames) throws IOException, NoSuchAlgorithmException, InvalidKeyException, MinioException {
        return minioService.downloadMultipleFilesAsZip(fileNames);
    }

    /**
     * 获取图片预览URL
     *
     * @param objectName 文件对象名
     * @return 预览 URL
     * @throws IOException
     * @throws NoSuchAlgorithmException
     * @throws InvalidKeyException
     * @throws MinioException
     */
    public String getImagePreviewUrl(String objectName) throws IOException, NoSuchAlgorithmException, InvalidKeyException, MinioException {
        return minioService.getImagePreviewUrl(objectName);
    }

    /**
     * 获取文件预览URL（支持多种文件类型）
     *
     * @param objectName 文件对象名
     * @return 预览 URL
     * @throws IOException
     * @throws NoSuchAlgorithmException
     * @throws InvalidKeyException
     * @throws MinioException
     */
    public String getFilePreviewUrl(String objectName) throws IOException, NoSuchAlgorithmException, InvalidKeyException, MinioException {
        return minioService.getFilePreviewUrl(objectName);
    }

    /**
     * 获取文件信息（用于预览判断）
     *
     * @param objectName 文件对象名
     * @return 文件信息
     * @throws IOException
     * @throws NoSuchAlgorithmException
     * @throws InvalidKeyException
     * @throws MinioException
     */
    public Map<String, Object> getFileInfo(String objectName) throws IOException, NoSuchAlgorithmException, InvalidKeyException, MinioException {
        return minioService.getFileInfo(objectName);
    }
}
